#include "SSRRenderPass.h"
#include <SEngine/Render/RenderFeature/UtilRenderFeature.h>
#include <SERender/Renderer/Renderer.h>
#include <SEForwardRenderPiplinePlugin/RenderTargetManager.h>
#include <SECore/Manager.h>
#include <SEUi/ImGui/imgui.h>
#include <SERender/Resourses/Loader/ShaderLoader.h>
#include <SEngine/Render/RenderFeature/UBORenderFeature.h>
#include <SERender/Descriptors/FrameDescriptor.h>

namespace SEngine
{
    SSRRenderPass::SSRRenderPass(Renderer & renderer)
        : RenderPass(renderer)
    {
        m_ssrMaterial = Manager::GetInstance().Get<Material>("SSR").value();
        m_ssrMaterial.Value().GetPSO().blend = true;
    }

    void SSRRenderPass::DrawFrame()
    {
        auto & frame = m_renderer.AnyDescriptor::GetDescriptor<FrameDescriptor>()->get();
        auto & rtManager = m_renderer.AnyDescriptor::GetDescriptor<RenderTargetManager>()->get();
        auto & utilRenderFeature = m_renderer.Descriptor<RenderFeature>::GetDescriptor<UtilRenderFeature>()->get();
        m_ssrMaterial.Value().Set("u_positionRT",  Shader::Sampler{*rtManager.GetTarget("PositionRT"), TextureSlot::TEXTURE1});
        m_ssrMaterial.Value().Set("u_normalRT",  Shader::Sampler{*rtManager.GetTarget("NormalRT"), TextureSlot::TEXTURE2});
        m_ssrMaterial.Value().Set("u_tangentRT",  Shader::Sampler{*rtManager.GetTarget("TangentRT"), TextureSlot::TEXTURE3});
        m_ssrMaterial.Value().Set("u_depthRT",  Shader::Sampler{*rtManager.GetTarget("DepthRT"), TextureSlot::TEXTURE4});
        m_ssrMaterial.Value().Set("u_albedoRT",  Shader::Sampler{*rtManager.GetTarget("AlbedoRT"), TextureSlot::TEXTURE5});
        utilRenderFeature.DrawBillboard(m_ssrMaterial);


        SignalUpdateUI.AddSlot([this]() {
            ImGui::PushID("SSRRenderPass");
            if (ImGui::CollapsingHeader("SSRRenderPass")) {
                if (ImGui::Button("Reload")) {
                    m_ssrMaterial.Value().GetShader().Reload([](auto & path, auto & name) {
                        ShaderLoader loader;
                        return loader.Load(path).CreateShader(EngineUniformBlockBinder());
                    });
                }
            }
            ImGui::PopID();
            return SlotState::Once;
        });

    }

}